//
//  EditMailViewController.m
//  SouthChina
//
//  Created by kc on 15/7/6.
//  Copyright (c) 2015年 科创. All rights reserved.
//

#import "EditMailViewController.h"
#import "PersonalInforViewController.h"

@interface EditMailViewController ()<UITextFieldDelegate>{
    NSString *sMailOld;
    NSString *slinkstatusOld;
}
@property (strong, nonatomic) IBOutlet UITextField *usermail;
@property (strong, nonatomic) IBOutlet UISwitch *btgroup;
@property (strong, nonatomic) IBOutlet UISwitch *btfriend;
@property (strong, nonatomic) UIButton * finishBtn;//完成按钮
@end

@implementation EditMailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self addTitleBarWithBackBtn:@"邮箱" needReturn:YES];
    UIView * viewBody = [[[NSBundle mainBundle] loadNibNamed:@"EditMailViewBody" owner:self options:nil] objectAtIndex:0];
    viewBody.frame = (CGRect){0, self.titlebar.frame.origin.y+self.titlebar.frame.size.height,SCREEN_WIDTH, SCREEN_HEIGHT-self.titlebar.frame.size.height};
    [self.view addSubview:viewBody];
    
    _usermail.delegate = self;
    //邮箱
    NSString *strmail = [DE getGlobalVar:LOGIN_USER_EMAIL] == nil?@"":[DE getGlobalVar:LOGIN_USER_EMAIL];
   _usermail.text = strmail;
    sMailOld = strmail;
    
    //NSString * linkinfostate = [DE getGlobalVar:[NSString stringWithFormat:@"%@%@",DE.userId,LINKINFOSTATE]];
    //LOGIN_USER_LINKINFOSTATE
     NSString *linkinfostate = [DE getGlobalVar:LOGIN_USER_LINKINFOSTATE] == nil?@"":[DE getGlobalVar:LOGIN_USER_LINKINFOSTATE];
    // 1、好友可以见 2 群组可以见（除好友，陌生人）3 任何人都可见 0 任何人都不可见
   if ([@"1" isEqualToString:linkinfostate]) {
        [_btfriend setOn:YES];
        [_btgroup setOn:NO];
        
    } else if ([@"2" isEqualToString:linkinfostate]) {
        [_btfriend setOn:NO];
        [_btgroup setOn:YES];
        
    } else if ([@"0" isEqualToString:linkinfostate]) {
        [_btfriend setOn:NO];
        [_btgroup setOn:NO];
        
    } else {
        [_btfriend setOn:YES];
        [_btgroup setOn:YES];
        
    }
    slinkstatusOld = linkinfostate;

}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return  YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

-(void)btnBack{
    
    NSRange range = [_usermail.text rangeOfString:@"@"];
    /////1010,需要判断邮箱的合法性
    if(range.length<=0 || ![self isEmail:_usermail.text])
    {
        //回退到上一级页面
        [self.navigationController popViewControllerAnimated:YES];
        PersonalInforViewController *PersonalInfoviewC = [self.navigationController.viewControllers objectAtIndex:(self.navigationController.viewControllers.count-1)];
        [PersonalInfoviewC showAlerHUD:@"修改失败，请输入正确的邮箱地址"];
        return;
    }
    
    
    NSString * linkStat = @"";
    
    bool isGroupButtonOn = [_btgroup isOn];
    bool isFriendButtonOn = [_btfriend isOn];
    
    
    if (isGroupButtonOn && isFriendButtonOn) {
        linkStat = @"3";
    }else if (isGroupButtonOn && !isFriendButtonOn)
    {
        linkStat = @"2";
    }else if (!isGroupButtonOn && isFriendButtonOn)
    {
        linkStat = @"1";
    }else
    {
        linkStat = @"0";
    }
    
    if([_usermail.text isEqualToString:sMailOld] && [linkStat isEqualToString:slinkstatusOld])
    {
        [super btnBack];
        return;
    }
    
    NSLog(@"linkStat。。。。。。%@",linkStat);
    
    NSDictionary *params = @{@"email":_usermail.text,@"linkInfoState":linkStat};
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    [DE serverCall:@"updateUserInfo" params:params callback:^(BOOL success, int code, NSString *desc, NSDictionary *data,NSDictionary*param) {
        //UIAlertView *view = [[UIAlertView alloc]initWithTitle:@"提示" message:@"" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if(success && [data objectForKey:@"result"])
        {
            [DE setGlobalVar:LOGIN_USER_EMAIL value:_usermail.text];
            [DE setGlobalVar:LOGIN_USER_LINKINFOSTATE value:linkStat];
            //view.message = @"信息提交成功！！";
           // [view show];
            //回退到上一级页面
            [self.navigationController popViewControllerAnimated:YES];
            PersonalInforViewController *PersonalInfoviewC = [self.navigationController.viewControllers objectAtIndex:(self.navigationController.viewControllers.count-1)];
            [PersonalInfoviewC showAlerHUD:@"修改邮箱成功"];
        }else
        {
            [self.navigationController popViewControllerAnimated:YES];
            PersonalInforViewController *PersonalInfoviewC = [self.navigationController.viewControllers objectAtIndex:(self.navigationController.viewControllers.count-1)];
            [PersonalInfoviewC showAlerHUD:@"修改邮箱失败"];
        }
        
    }];
    [_usermail resignFirstResponder]; //点击完成按钮，键盘隐藏
}

//邮箱合法性验证 1010
- (BOOL) isEmail: (NSString *) emailAddress {
    NSString *regex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    return [emailTest evaluateWithObject:emailAddress];
}


@end
